<?php
set_time_limit(0); //防止执行超时
ob_end_clean();   //清空并关闭输出缓冲区

if(!isset($_GET["id"])){
    echo "pls input id!";
    return;
}

include("base_function.php");
include("base/db.php");

function analyse(&$html,$good_id)
{
    //仓储所在地，用户ID，用户url，销量、标题、分类、页面详细地址、价格
    //用户ID
    //用户地址
    $reg = "|<a href=\"(.*?)\" title=\"Member ID:&nbsp;(.*?)\">|";
    $match_count = preg_match ($reg,$html,$out);
    if($match_count == 0){
        $out[1] = 0;
        $out[2] = 0;
    }
    $good_info["useurl"] =  $out[1] ;
    $good_info["usename"] =  $out[2] ;
    //销量
    $reg = "|<a href=\"(.*?)\">([\d,]*?) sold</a>|";
    $match_count = preg_match ($reg,$html,$out);
    if($match_count == 0){
        $out[1] = 0;
        $out[2] = 0;
    }
    $good_info["sold_url"] = $out[1] ;
    $good_info["sold"] = str_replace(",","",$out[2]) ;
    //仓储所在地
    $reg = "|Item location:</div><div class=\"iti-eu-bld-gry \">(.*?)</div>|";
    $match_count = preg_match ($reg,$html,$out);
    if($match_count == 0){
        $out[1] = 0;
    }
    $good_info["location"] = $out[1] ;
    //标题
    $reg = "|<title> (.*?)</title>|";
    $match_count = preg_match ($reg,$html,$out);
    if($match_count == 0){
        $out[1] = 0;
    }
    $good_info["title"] = $out[1] ;
    
    //价格
    $reg = '|<span class="notranslate" id="prcIsum" itemprop="price"  style="">(.*?)</span>|';
    $match_count = preg_match ($reg,$html,$out);
    if($match_count == 0){
        $out[1] = 0;
    }
    $good_info["price"] = str_replace(",","",$out[1]) ;
    //watchers
    $reg = '|<span class="vi-buybox-watchcount">(.*?)</span> watchers</span>|';
    $match_count = preg_match ($reg,$html,$out);
    if($match_count == 0){
        $out[1] = 0;
    }
    $good_info["watchers"] = str_replace(",","",$out[1]) ;
    //Feedback score
    $reg = '|"Feedback score: (.*?)"|';
    $match_count = preg_match ($reg,$html,$out);
    if($match_count == 0){
        $out[1] = 0;
    }
    $good_info["usescore"] = str_replace(",","",$out[1]) ;
    
    $lies = "id";
    $vals = "'$good_id'";
    foreach($good_info as $key => $value)
    {
        $lies .= ",$key";
        $vals .= ",'$value'";
    }
    exec_db ( "insert into pm_good_info ($lies) values ($vals)\r\n");
}
function doit()
{
    open_db();
    $good_urls = query_db("select * from pm_product where catalog_id = ".$_GET["id"]." and id not in (select id from pm_good_info)");
    foreach ($good_urls as $good_url){
        //echo $good_url["url"]."\r\n";
        $html = http_response($good_url["url"]);
        $html = htmlspecialchars_decode($html,ENT_QUOTES);
        //file_put_contents("test.txt",$html);
        analyse($html,$good_url["id"]);
        //break;
    }
    close_db();
    return;
}

doit();
echo "ok";

?>
